package com.android.logcat;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author GYL
 * @version 1.0
 * @dateTime 2020/6/30-14:22
 * @description
 */
/*public*/ class Logger implements ILogger {
  
  /**
   * StackTrace 层次
   */
  private static final int MIN_STACK_OFFSET = 6;
  private ILogger loggerProxy;
  
  Logger(LoggerConfig loggerConfig){
    LoggerRunnable loggerThread = loggerConfig.loggerThread();
    loggerThread.on();
    loggerProxy=loggerConfig.getLoggerProxy();
  }
  
  @Override
  public void v(String tag, Object msg, Throwable e) {
    String[] strs=generateMsg(LoggerLevel.VERBOSE,tag,msg,e);
    if(loggerProxy!=null){
      loggerProxy.v(strs[0],strs[1],e);
    }else{
      Log.v(strs[0],strs[1],e);
    }
  }
  
  @Override
  public void d(String tag, Object msg, Throwable e) {
    String[] strs=generateMsg(LoggerLevel.DEBUG,tag,msg,e);
    if(loggerProxy!=null){
      loggerProxy.d(strs[0],strs[1],e);
    }else{
      Log.d(strs[0],strs[1],e);
    }
  }
  
  @Override
  public void i(String tag, Object msg, Throwable e) {
    String[] strs=generateMsg(LoggerLevel.INFO,tag,msg,e);
    if(loggerProxy!=null){
      loggerProxy.i(strs[0],strs[1],e);
    }else{
      Log.i(strs[0],strs[1],e);
    }
  }
  
  @Override
  public void w(String tag, Object msg, Throwable e) {
    String[] strs=generateMsg(LoggerLevel.WARM,tag,msg,e);
    if(loggerProxy!=null){
      loggerProxy.w(strs[0],strs[1],e);
    }else{
      Log.w(strs[0],strs[1],e);
    }
  }
  
  @Override
  public void e(String tag, Object msg, Throwable e) {
    String[] strs=generateMsg(LoggerLevel.ERROR,tag,msg,e);
    if(loggerProxy!=null){
      loggerProxy.e(strs[0],strs[1],e);
    }else{
      Log.e(strs[0],strs[1],e);
    }
  }
  
  @SuppressLint("NewApi")
  @Override
  public void wtf(String tag, Object msg, Throwable e) {
    String[] strs=generateMsg(LoggerLevel.WTF,tag,msg,e);
    if(loggerProxy!=null){
      loggerProxy.wtf(strs[0],strs[1],e);
    }else{
      Log.wtf(strs[0],strs[1],e);
    }
  }
  
  @Override
  public void println(Object msg) {
    String[] strs=generateMsg(LoggerLevel.SYSTEMOUT,null,msg,null);
    if(loggerProxy!=null){
      loggerProxy.println(strs[1]);
    }else{
      Log.i(strs[0]+"_System.out",strs[1]);
    }
  }
  
  @SuppressLint("DefaultLocale")
  protected synchronized String[] generateMsg(@LoggerLevel String type, String tag, Object obj, Throwable t) {
    StackTraceElement caller = Thread.currentThread().getStackTrace()[LoggerLevel.SYSTEMOUT.equals(type)?MIN_STACK_OFFSET-1:MIN_STACK_OFFSET];
    if (TextUtils.isEmpty(tag)) {
      String callerClazzName = caller.getClassName();
      tag = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
    }
    tag=LoggerConfig.getInstance().getTagPrefix()+"_"+tag;
    StringBuilder sb = new StringBuilder("");
    sb.append("(")
      .append(caller.getFileName())
      .append(":")
      .append(caller.getLineNumber())
      .append("):[")
      .append(caller.getMethodName())
      .append("]:: ")
      .append(obj)
      .append(t == null ? "" : "( " + t.getMessage() + " )");
    String msg = sb.toString();
    //    int index = 0;
    //    int maxLength = msg.length();
    //    String finalString = "";
    //    String log_type = "D";
    //    while (index < maxLength) {
    //      if (maxLength <= index + 4000) {
    //        finalString = msg.substring(index);
    //      } else {
    //        finalString = msg.substring(index, index + 4000);
    //      }
    //      index += 4000;
    //      log_type = printLog(type, tag, finalString, t);
    //    }
    if(LoggerConfig.getInstance().isEnableWriteFile()){
      LoggerContent content=new LoggerContent(type,tag,msg,t);
      LoggerRunnable loggerThread = LoggerConfig.getInstance().loggerThread();
      loggerThread.addLogger(content);
    }
    return new String[]{tag,msg};
  }
  
}
